﻿/**
 * Copyright (c) 2013 Nokia Corporation.
 */

using Microsoft.Phone.Maps.Controls;
using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Dradis
{
    /// <summary>
    /// Container for user data.
    /// </summary>
    class User
    {
        // Data types
        public enum ObjectType {
            Raider = 0,
            Viper = 1,
            Unknown = 2
        };

        // Constants
        private const String RaiderMarkerImageUri = "/Graphics/raider-48.png";
        private const String ViperMarkerImageUri = "/Graphics/viper-48.png";
        private const String UnknownMarkerImageUri = "/Graphics/unknown-48.png";

        // Members and properties

        public String UserName { get; set; }

        private GeoCoordinate _location = null;
        public GeoCoordinate Location
        {
            get
            {
                return _location;
            }
            set
            {
                if (_location != value)
                {
                    _location = value;

                    if (Overlay == null)
                    {
                        CreateMapOverlay();
                    }
                    else
                    {
                        Overlay.GeoCoordinate = _location;
                    }

                    EventHandler handler = LocationChanged;

                    if (handler != null)
                    {
                        handler(this, EventArgs.Empty);
                    }
                }
            }
        }

        private ObjectType _type;
        public ObjectType Type
        {
            get
            {
                return _type;
            }
            set
            {
                if (_type != value)
                {
                    _type = value;
                    CreateMapOverlay();
                }
            }
        }

        public MapOverlay Overlay
        {
            get;
            private set;
        }

        public event EventHandler LocationChanged;

        /// <summary>
        /// Constructor.
        /// </summary>
        public User()
        {
            UserName = null;
            _type = ObjectType.Unknown;
            Overlay = null;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="location"></param>
        public User(String userName, GeoCoordinate location, ObjectType type)
        {
            UserName = userName;
            Type = type;
            Location = location;
        }

        /// <summary>
        /// Creates the map overlay for the user.
        /// </summary>
        private void CreateMapOverlay()
        {
            // Create the marker image
            Image markerImage = new Image();
            BitmapImage bitmapImage = new BitmapImage();
            String uri = null;

            switch (_type)
            {
                case User.ObjectType.Raider:
                    uri = RaiderMarkerImageUri;
                    break;
                case User.ObjectType.Viper:
                    uri = ViperMarkerImageUri;
                    break;
                default:
                    uri = UnknownMarkerImageUri;
                    break;
            }

            bitmapImage.UriSource = new Uri(uri, UriKind.Relative);
            markerImage.Source = bitmapImage;
            markerImage.Height = MainPage.ContentHeight
                * MainPage.UserLocationMarkerRelativeHeight;

            TextBlock textBlock = new TextBlock();
            textBlock.Text = UserName.ToUpper();
            SolidColorBrush brush = new SolidColorBrush();
            Color textColor = new Color();
            textColor.A = 255;
            textColor.R = 220;
            textColor.G = 255;
            textColor.B = 255;
            brush.Color = textColor;
            textBlock.Foreground = brush;
            textBlock.FontWeight = FontWeights.Bold;
            Thickness margin = new Thickness();
            margin.Top = MainPage.ContentHeight * 0.1;
            textBlock.Margin = margin;

            Grid grid = new Grid();
            grid.Children.Add(markerImage);
            grid.Children.Add(textBlock);
            
            // Create the marker itself
            MapOverlay overlay = new MapOverlay();
            overlay.Content = grid; // markerImage;
            overlay.GeoCoordinate = _location;
            overlay.PositionOrigin = new Point(0.5, 0.5);
            Overlay = overlay;
        }
    }
}
